Adding a custom bulk action in WordPress list tables

custom bulk action in WordPress list tables

WordPress have default option to select multiple posts or page at once and perform the bulk edit and delete action. Using this feature you can anything you want onto the selected elements. Please refer the below image

add custom bulk action

If you are a WordPress developer and want to add your own custom bulk action for post, page, or custom post type then you must need to read this article.

Suggested Topic: Remove “Archive:”, “Category:”, “Tag:”, “Author:” from the_archive_title in WordPress

Adding a custom bulk action in WordPress

In this article, you will get the complete step to create your own custom bulk action in the WordPress admin dropdown list. For this, we apply the filter bulk_actions_<screen>, where <screen> is the name of the admin screen you want to use.

Please find the below complete possible options for the screen:

  • bulk_actions-edit-post: Post type ‘post’ edit screen
  • bulk_actions-edit-page: Post type ‘page’ edit screen
  • bulk_actions-edit-<post-type-name>: Custom post type edit screen
  • bulk_actions-edit-<custom-taxonomy>: Custom taxonomy edit screen
  • bulk_actions-edit-comments: Comments list screen
  • bulk_actions-plugins: Plugins list screen
  • bulk_actions-users: Users list screen
  • bulk_actions-upload: Media library list (only works in list view, not grid view)

The above filter is applied onto an array with key and value pairs; where the key is a unique key for actions, and value is the label that appears in the dropdown.

Let’s use the below example. Here we will create a bulk action for Move to Archive multiple posts at once.

Once you’ve added the code above to your theme functions.php file, you should get the result below:

Adding a custom bulk action in WordPress

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

How to handle custom bulk action

Now we will use the filter handle_bulk_actions-<screen> to trigger action to do something as per our newly created bulk action. Just do not forget to replace <screen> it with an admin screen. In the below code we going to replace <screen> with the post. Copy and paste the below code to your theme functions.php file

In the above code, you will get three possible arguments in this filter. 

  1. $redirect_url : is actually an URL that WordPress should redirect to after it’s finished with your bulk action.
  2. $action : is the name of the action that was chosen in the dropdown.
  3. $post_ids : is an array of all element IDs that were checked for the bulk action

Show a custom notice after our custom bulk action

The last step is to define custom notices. This allows us to add a custom admin notice which triggers if ‘move-to-archive’ is set. You can set admin notice something like “2 posts moved to archive” where “2” is the number of posts we applied our action onto or create a more detailed message.

In order to show admin notices, we will use the action admin_notices.

That’s how it looks:

Show a custom notice after our custom bulk action in wordpress

Thanks for reading 🙏  I hope you found this tutorial helpful for your project. Keep learning!.

Are you want to get implementation help, or modify or extend the functionality of this script? submit your paid request

Related posts